package com.dragon.manage.action;

import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.service.IManageAgentService;
import com.dragon.manage.service.IManageService;
import com.dragon.manage.util.LogUtil;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.RightUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("rawtypes")
public class ManageAgentAction extends BaseAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private IManageService manageService;

	private IManageAgentService manageAgentService;
	
	public void setManageService(IManageService manageService) {
		this.manageService = manageService;
	}

	public void setManageAgentService(IManageAgentService manageAgentService) {
		this.manageAgentService = manageAgentService;
	}
	
	/**
	 * 代理列表
	 * @return
	 */
	public String agentList(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String page=request.getParameter("page");
			if(page==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageAgentService.agentList(StringUtil.getParam(page));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("获取代理列表异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 冻结代理
	 * @return
	 */
	public String lockAgent(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String agentId=request.getParameter("agentId");
			String type = request.getParameter("type");
			if(agentId==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageAgentService.lockAgent(StringUtil.getParam(agentId),StringUtil.getParam(type));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("冻结代理异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 代理提现申请列表
	 * @return
	 */
	public String getAgentWithdrawList(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String agentId=request.getParameter("agentId");
			String type=request.getParameter("type");
			String page=request.getParameter("page");
//			if(agentId==null){
//				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
//				ct.put(StaticData.reKey.result.getKey(), result);
//				return StaticData.RE_SUCCESS;
//			}
			
			result = manageAgentService.getAgentWithdrawList(StringUtil.getParam(agentId),
					StringUtil.getParam(type),StringUtil.getParam(page));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("代理提现申请列表", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 代理收益明细
	 * @return
	 */
	public String getAgentEarningsDetail(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String agentId=request.getParameter("agentId");
			String page=request.getParameter("page");
			if(agentId==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageAgentService.getAgentEarningsDetail(StringUtil.getParam(agentId),
					StringUtil.getParam(page));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("代理收益明细", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 审核提现
	 * @return
	 */
	public String checkWithdraw(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			/*
			 * 提现id和审核结果状态
			 */
			String financeid=request.getParameter("financeid");
			String type=request.getParameter("type");
			String auditremarks=request.getParameter("auditremarks");
			if(financeid==null||financeid.isEmpty()||type==null||type.isEmpty()){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result=manageAgentService.checkWithdraw(StringUtil.getParam(financeid), 
					StringUtil.getParam(type), manageUser,auditremarks);
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("代理收益明细", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 修改代理信息
	 * @return
	 */
	public String updateAgentInfo(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			/*
			 * 提现id和审核结果状态
			 */
			String agentId=request.getParameter("agentId");
			String phone=request.getParameter("phone");
			String name=request.getParameter("name");
			if(agentId==null||agentId.isEmpty()){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			
			result = manageAgentService.updateAgentInfo(StringUtil.getParam(agentId), phone, name);
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("修改代理信息", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
}
